package com.zzedu.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.zzedu.annon.AccessPermission;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.enums.OperatorType;
import com.zzedu.common.enums.UserTypeEnumns;
import com.zzedu.common.utils.file.FileUtils;
import com.zzedu.common.utils.uuid.IdUtils;
import com.zzedu.domain.ZzUploadFile;
import com.zzedu.enmuns.FileRelationTypeEnum;
import com.zzedu.integration.oss.IOssClientService;
import com.zzedu.integration.oss.dto.FileInfoDTO;
import com.zzedu.integration.oss.dto.OssFileResult;
import com.zzedu.params.UploadFileParams;
import com.zzedu.result.Result;
import com.zzedu.result.ResultEnum;
import com.zzedu.service.IZzUploadFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 资源管理Controller
 *
 * @author Mr.Wang
 * @date 2025-01-30
 */
@RestController
@RequestMapping("/fileresource")
@AccessPermission(value = {UserTypeEnumns.STUDENT})
public class ApiUploadFileController {
    private static final Logger log = LoggerFactory.getLogger(ApiUploadFileController.class);
    @Autowired
    IOssClientService ossClientService;


    @Autowired
    IZzUploadFileService zzUploadFileService;


    /**
     * 上传文件
     *
     * @param uploadFileParams
     * @return
     */
    @Log(title = "文件-上传文件", businessType = BusinessType.APP_STUDENT, operatorType = OperatorType.MOBILE)
    @PostMapping("/upload")
    public Result<FileInfoDTO> upload(@Validated UploadFileParams uploadFileParams) throws IOException {
        Result<FileInfoDTO> result = new Result();
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        FileRelationTypeEnum fileRelationTypeEnum = uploadFileParams.getRelationType();
        MultipartFile file = uploadFileParams.getFile();
        JSONObject openParams = new JSONObject();
        openParams.put("fileName", uploadFileParams.getFile().getName());
        OssFileResult<FileInfoDTO> fileUploadResult = ossClientService.uploadFile(file.getBytes(), fileRelationTypeEnum.getOssPath(), file.getOriginalFilename(), IdUtils.fastSimpleUUID());
        if (fileUploadResult.isSuccess()) {
            FileInfoDTO fileInfoDTO = fileUploadResult.getData();
            ZzUploadFile uploadFile = new ZzUploadFile();
            uploadFile.setRelationType(fileRelationTypeEnum.getCode());
            uploadFile.setUrl(fileInfoDTO.getUrl());
            uploadFile.setOldFileName(fileInfoDTO.getOriginalFilename());
            uploadFile.setFileName(FileUtils.getName(fileInfoDTO.getNewFileName()));
            uploadFile.setRelationId(uploadFileParams.getRelationId());
            uploadFile.setFileExt(fileInfoDTO.getFileSuffix());
            uploadFile.setSourceModel(fileRelationTypeEnum.getCode());
            uploadFile.setSize(fileInfoDTO.getSize());
            uploadFile.setSortNum(uploadFile.getId());
            zzUploadFileService.save(uploadFile);
            fileInfoDTO.setFileId(uploadFile.getId());
            result.setData(fileInfoDTO);
        } else {
            result.setResultCode(ResultEnum.FAIL.getCode());
            result.setResultMessage(ResultEnum.FAIL.getDesc());
        }
        return result;
    }
}
